<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #ball{width: 100px;height: 100px;background-color: red;border-radius:50%;margin-left: 0px;}
        #ball2{width: 100px;height: 100px;background-color: red;border-radius:50%;margin-left: 0px;}
    </style>
</head>
<body>
    <div id="ball"></div>
    <div id="ball2"></div>
    <input type="button" value="球1" onclick="move1('ball',500)">
    <input type="button" value="球2" onclick="move2('ball2',1000)">
    <script>
        var ball = document.getElementById('ball');
        var ball2 = document.getElementById('ball');
        var move1 = go(20);
        var move2 = go(40);
        function go(fps){//frame per second
            var t;//闭包
            function myMove(id,end){
                var htmlObj = document.getElementById(id);
                clearInterval(t);
                var offset = 1;     //速度
                function move(){
                    var cssObj = getComputedStyle(htmlObj);
                    var originL = parseInt(cssObj.marginLeft);
                    if (originL>=end){
                        clearInterval(t);
                    } else {
                        offset = Math.ceil((end-originL)/45);
                        console.log(offset);
                        htmlObj.style.marginLeft = originL + offset + 'px';
                    }
                }
                var m = 1000/fps; // 计算动画效果每秒多少帧，用于设置计时器
                t = setInterval(move,m);//1
            }
            return myMove;
        }
    </script>
</body>
</html>